package com.risen.project.enums;

import cn.hutool.core.util.StrUtil;

import java.util.Arrays;
import java.util.Optional;

/**
 * 预算来源枚举
 */
public enum CapitalBudgetEnum {

    CBE_CAPITAL_OWNER("1", "自有资金"),

    CBE_CAPITAL_INVEST_SAME_LEVEL("2", "政府投资-本级财政资金"),

    CBE_CAPITAL_INVEST_SUPER_LEVEL("3", "政府投资-上级补助资金"),

    CBE_CAPITAL_LOANS("4", "银行贷款"),

    CBE_CAPITAL_OTHERS("5", "其他资金"),
    ;

    private final String code;
    private final String msg;

    CapitalBudgetEnum(String code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public static String getMsgByCode(String code) {
        Optional<CapitalBudgetEnum> optional = Arrays.stream(values())
                .filter(c -> c.code.equals(code))
                .findFirst();

        return optional.isPresent() ? optional.get().msg : "";
    }

    /**
     * 分割code拼接枚举结果方法
     * @param code 匹配code
     * @param split 分隔符
     * @return String
     */
    public static String getMsgByCodeSplit(String code, String split) {
        if (StrUtil.isEmpty(code)) {
            return "";
        }

        StringBuilder sb = new StringBuilder();
        String[] codeSplit = StrUtil.split(code, split);

        for (String cs : codeSplit) {
            Optional<CapitalBudgetEnum> optional = Arrays.stream(values())
                    .filter(c -> c.code.equals(cs))
                    .findFirst();

            optional.ifPresent(projectStatusEnum -> sb.append(projectStatusEnum.msg).append(split));
        }

        return sb.toString();
    }
}
